/// @tags:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

namespace BlueQuantum {

typedef long long ll;
typedef unsigned long long ull;

int const N = 1e2 + 5;
int const vx[] = {0, 0, -1, 1}, vy[] = {-1, 1, 0, 0}, p[] = {2, 3, 5, 7, 11, 13, 17, 19};

int n, m;
ll a[N][N];

inline ll qmul(ll a, ll b, ll mod) {
  ll c = a * b - (ll)((long double)a * b / mod) * mod;
  return c < 0 ? c + mod : (c >= mod ? c - mod : c);
}

inline ll qpow(ll base, ll exp, ll mod) {
  ll res = 1;
  while (exp) {
    if (exp & 1) res = qmul(res, base, mod);
    base = qmul(base, base, mod);
    exp >>= 1;
  }
  return res;
}

inline bool check(ll a, ll md, ll exp) {
  static ll tmp;
  if ((tmp = qpow(a, exp, md)) == md - 1) return true;
  if (tmp == 1) return exp & 1 ? true : check(a, md, exp >> 1);
  return false;
}

inline bool miller(ll x) {
  if (x == 2) return true;
  if ((x & 1) == 0 || x == 1) return false;
  for (int i = 0; i < 8; ++i) {
    if (x == p[i]) return true;
    if (!check(p[i], x, x - 1)) return false;
  }
  return true;
}

inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

inline ll f(ll x, ll const& c, ll const& n) { return (qmul(x, x, n) + c) % n; }

typedef long long ll;
typedef unsigned long long ull;
ull mul(ull a, ull b, ull p) {
  ull c = 0;
  for (; b; b >>= 13) (c += a * (b & 8191)) %= p, a = (a << 13) % p;
  return c;
}
ull pw(ull a, ull n, ull p) {
  ull b = 1;
  for (; n; n >>= 1) n % 2 ? b = mul(b, a, p) : 1, a = mul(a, a, p);
  return b;
}

bool isc(ll x) {
  if (x < 4) return 0;
  if (x % 2 == 0) return 1;
  if (x == 46856248255981ll) return 1;
  const int a[5] = {2, 3, 7, 61, 24251};
  for (int i = 0; i < 5 && a[i] < x; i++) {
    ll y = x - 1, t, u;
    while (y % 2 == 0) y /= 2;
    for (t = pw(a[i], y, x); y < x; y *= 2, t = u)
      if ((u = mul(t, t, x)) == 1 && t != 1 && t != x - 1) return 1;
    if (t != 1) return 1;
  }
  return 0;
}

struct edge {
  int to, cap;
  edge *rev, *next;
} E[80010], *ne = E, *fir[10010], *cur[10010];
void link(int a, int b, int c) {
  *ne = (edge){b, c, ne + 1, fir[a]};
  fir[a] = ne++;
  *ne = (edge){a, 0, ne - 1, fir[b]};
  fir[b] = ne++;
}
int Q[10010], D[10010];
int dfs(int i, int c) {
  if (i == 1 || !c) return c;
  int fl = 0, f;
  for (edge*& e = cur[i]; e && c; e = e->next)
    if (e->cap && D[e->to] == D[i] - 1 && (f = dfs(e->to, c < e->cap ? c : e->cap)))
      fl += f, e->cap -= f, e->rev->cap += f, c -= f;
  return fl;
}

int maxf() {
  for (int f = 0;; f += dfs(0, 1 << 30)) {
    memset(D, 0, sizeof(*D) * N);
    memcpy(cur, fir, sizeof(*cur) * N);
    for (int *h = Q, *t = Q + (D[*Q = 1] = 1); h < t; h++)
      for (edge* e = fir[*h]; e; e = e->next)
        if (e->rev->cap && !D[e->to]) D[*t++ = e->to] = D[*h] + 1;
    if (!*D) return f;
  }
}

bool vis[2][10010];

void ff(int i, int t) {
  vis[t][i] = 1;
  for (edge* e = fir[i]; e; e = e->next)
    if (!vis[t][e->to])
      if (t ? e->rev->cap : e->cap) ff(e->to, t);
}

int k, x[10010], y[10010];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) cin >> a[i][j];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      if (a[i][j] > -1) {
        if (i + j & 1) link(i * m + j + 2, 1, 1);
        else {
          link(0, i * m + j + 2, 1);
          for (int d = 0, dx, dy; d < 4; d++)
            if ((dx = i + vx[d]) >= 0 && dx < n && (dy = j + vy[d]) >= 0 && dy < m &&
                a[dx][dy] > -1 && isc(a[dx][dy] + a[i][j]))
              link(i * m + j + 2, dx * m + dy + 2, 1);
        }
      }
  }
  maxf();
  ff(0, 0);
  ff(1, 1);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] > -1)
        for (edge* e = fir[i * m + j + 2]; e; e = e->next)
          if (e->cap == (i + j & 1) && (e->to < 2 || vis[i + j & 1][e->to]))
            x[k] = i + 1, y[k++] = j + 1;
  cout << k << '\n';
  for (int i = 0; i < k; i++) cout << x[i] << ' ' << y[i] << '\n';
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("game.in", "r", stdin);
  freopen("game.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
